package com.wd.mine.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.amap.api.location.AMapLocationClient
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.AMap.OnCameraChangeListener
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.MapView
import com.amap.api.maps2d.MapsInitializer
import com.amap.api.maps2d.model.CameraPosition
import com.amap.api.maps2d.model.LatLng
import com.amap.api.maps2d.model.MarkerOptions
import com.blankj.utilcode.util.ToastUtils
import com.wd.common.base.BaseActivity
import com.wd.common.bean.NearbyChargeStationBean
import com.wd.common.viewmodel.MyViewModel
import com.wd.mine.adapter.ChargeStationBeanAdapter
import com.wd.mine.callBack.OnItemClick
import com.wd.mine.databinding.AbroadmapActivityBinding
import java.io.File


class AbroadMapSwitchActivity : BaseActivity<AbroadmapActivityBinding,Any>() {
    private var aMap: AMap? = null
    private var mapView: MapView? = null
    private var position: LatLng? = null
    private val viewModel : MyViewModel by viewModels()
    private var list = ArrayList<NearbyChargeStationBean.Result>()

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mapView = binding.map
        mapView!!.onCreate(savedInstanceState) // 此方法必须重写
        init()
        MapsInitializer.loadWorldGridMap(true)
        //回调
        val callBack = object : OnItemClick {
            override fun onClick(location : String, name : String)  {
                val split = location.split(",")
                position = LatLng(split[1].toDouble(),split[0].toDouble())
                move(null)
                val markerOption = MarkerOptions()
                markerOption.position(position)
                markerOption.title(name).snippet(location)
                aMap!!.addMarker(markerOption)
            }
            //去高德地图app
            override fun toAMap(place : String, location : String) {
                val intent = Intent()
                val split = location.split(",")
                intent.data = Uri.parse(
                    "androidamap://route?sourceApplication=car&dname=$place&dlat="+split[1]+"&dlon="+split[0]+"&t=0"
                )
                // 启动调用
                startActivity(intent)
            }
        }
        var stationAdapter = ChargeStationBeanAdapter(this,list,callBack)
        binding.rec.layoutManager = LinearLayoutManager(this@AbroadMapSwitchActivity)
        binding.rec.adapter = stationAdapter
        //  40.052505 / 116.162347    116.411858,39.916260
        viewModel.nearChargeStation("116.162347,40.052505")

        viewModel.nearChargeStation.observe(this){
            it?.let {
                list.addAll(it.result)
                stationAdapter.notifyDataSetChanged()
                ToastUtils.showShort(it.result.size)

            }
        }
    }

    override fun getViewBinding()= AbroadmapActivityBinding.inflate(layoutInflater)

    override fun initData() {
        //没用，但必须重写，  相关的代码在onCreate中
    }

    /**
     * 初始化AMap对象
     */
    private fun init() {
        if (aMap == null) {
            aMap = mapView!!.map
            setUpMap()
        }
    }

    /**
     * 对地图添加onMapIsAbroadListener
     */
    private fun setUpMap() {
        aMap!!.setOnCameraChangeListener(object : OnCameraChangeListener {
            override fun onCameraChangeFinish(cameraPosition: CameraPosition) {}
            override fun onCameraChange(cameraPosition: CameraPosition) {}
        })
    }

    /**
     * 方法必须重写
     */
    override fun onResume() {
        super.onResume()
        mapView!!.onResume()
    }

    /**
     * 方法必须重写
     */
    override fun onPause() {
        super.onPause()
        mapView!!.onPause()
    }

    /**
     * 方法必须重写
     */
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView!!.onSaveInstanceState(outState)
    }

    /**
     * 方法必须重写
     */
    override fun onDestroy() {
        super.onDestroy()
        mapView!!.onDestroy()
    }



    //定位发起端
    private val mLocationClient: AMapLocationClient? = null

    //导航
//    fun openNavigation(latitude: Double, longitude: Double) {
//        try {
//            val mSlat = mLocationClient!!.lastKnownLocation.latitude
//            val mSlon = mLocationClient.lastKnownLocation.longitude
//            if (isInstallByread("com.autonavi.minimap")) {
//                val intent = Intent()
//                intent.data = Uri.parse(
//                    "androidamap://route?sourceApplication=softname&slat="
//                            + mSlat + "&slon=" + mSlon + "&sname=起点&dlat=" + latitude + "&dlon="
//                            + longitude + "&dname=终点&dev=0&m=0&t=1&showType=1"
//                )
//                // 启动调用
//                startActivity(intent)
//            } else if (isInstallByread("com.baidu.BaiduMap")) {
//                val wellLocation: DoubleArray = GPSUtil.gcj02_To_Bd09(latitude, longitude)
//                val location: DoubleArray = GPSUtil.gcj02_To_Bd09(mSlat, mSlon)
//                val intent = Intent()
//                intent.data = Uri.parse(
//                    "baidumap://map/direction?region="
//                            + mLocationClient.lastKnownLocation.city + "&origin="
//                            + location[0] + "," + location[1] + "&destination=" + wellLocation[0]
//                            + "," + wellLocation[1] + "&mode=driving"
//                )
//                startActivity(intent)
//            } else {
//                ToastUtils.showShort("请安装高德或百度地图")
//                val uri = Uri.parse("market://details?id=com.autonavi.minimap")
//                val intent = Intent(Intent.ACTION_VIEW, uri)
//                startActivity(intent)
//            }
//        } catch (e: Exception) {
//            Log.e("openNavigation: ", e.toString())
//            ToastUtils.showShort("请检查是否开启定位权限")
//        }
//    }

    fun move(v: View?) {
        //定位
        aMap!!.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14f))
        aMap!!.invalidate() // 刷新地图
    }
}